package com.bojin.studio.ubattery;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;








import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;


public class ControlManager {
	private Context mcontext;
	private WifiManager wifiManager;
	private BluetoothAdapter mBluetoothAdapter;
	
	public ControlManager(Context context)
	{
		mcontext=context;
		wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
	}
	
	public void setWiFiEnable(boolean enable)
	{
		
		if(enable)
		{
			wifiManager.setWifiEnabled(true);
		}
		else
		{
			wifiManager.setWifiEnabled(false);
		}
	}
	
	public void setMobileEnable (boolean enabled) {
	     ConnectivityManager conMgr = (ConnectivityManager) mcontext.getSystemService(Context.CONNECTIVITY_SERVICE);

	     Class<?> conMgrClass = null; 
	     Field iConMgrField = null; 
	     Object iConMgr = null; 
	     Class<?> iConMgrClass = null; 
	     Method setMobileDataEnabledMethod = null; 

	     try {
	      
	    	 conMgrClass = Class.forName(conMgr.getClass().getName());	      
	    	 iConMgrField = conMgrClass.getDeclaredField("mService");	      
	    	 iConMgrField.setAccessible(true);
	    	 iConMgr = iConMgrField.get(conMgr);	      
	    	 iConMgrClass = Class.forName(iConMgr.getClass().getName());
	       	 setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
	         setMobileDataEnabledMethod.setAccessible(true);
	         setMobileDataEnabledMethod.invoke(iConMgr, enabled);
	     } 
	     catch (ClassNotFoundException e) {
	    	 e.printStackTrace();
	     } catch (NoSuchFieldException e) {
	    	 e.printStackTrace();
	     } catch (SecurityException e) {
	    	 e.printStackTrace();
	     } catch (NoSuchMethodException e) {
	    	 e.printStackTrace();
	     } catch (IllegalArgumentException e) {
	    	 e.printStackTrace();
	     } catch (IllegalAccessException e) {
	    	 e.printStackTrace();
	     } catch (InvocationTargetException e) {
	    	 e.printStackTrace();
	     }
	};
	
	public void setFlyEnable(boolean enable)
	{
		
		if(enable)
		{
			Settings.System.putInt(mcontext.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 1);
			Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
			intent.putExtra("state", true);
			mcontext.sendBroadcast(intent);
		}
		else
		{
			Settings.System.putInt(mcontext.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0);
			Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
			intent.putExtra("state", false);
			mcontext.sendBroadcast(intent);
		}
	}
	
	public void setAutoBrightnessEnable(boolean enable)
	{
		
		if(enable)
		{
			Settings.System.putInt(mcontext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
			ModifyBrightness(mcontext);
		}
		else
		{
			Settings.System.putInt(mcontext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
		}
	}
	
	public void ModifyBrightness(Context context)
	{
			WindowManager.LayoutParams layoutParams = ((Activity) context).getWindow().getAttributes();
			layoutParams. screenBrightness = LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
			((Activity) context).getWindow().setAttributes(layoutParams);
	}
	
	public void setSyncEnable(boolean enable)
	{
		
		if(enable)
		{
			ContentResolver.setMasterSyncAutomatically(true);
		}
		else
		{
			ContentResolver.setMasterSyncAutomatically(false);
		}
	}
	
	public void adjustBrightness(int i)
	{
		
		
	}
	
	public boolean getMobileState()
	{
		 final TelephonyManager telephonyManager = (TelephonyManager) mcontext.getSystemService(Context.TELEPHONY_SERVICE);
	     if(telephonyManager.getDataState()==(TelephonyManager.DATA_CONNECTED))
	     {
	    	 return true;
	     }
	     else
	     {
	    	 return false;
	     }
	}
	
	public boolean getWiFiState()
	{
		if(wifiManager.isWifiEnabled())
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
	}
	
	public boolean getAutoBrightnessState()
	{
		if(Settings.System.getInt(mcontext.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE, 0) == 1)
    	{
    		return true;
    	}
    	else
    	{
    	    return false;
    	}
	}
	
	public boolean getFlyState()
	{
		if(Settings.System.getInt(mcontext.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) == 1)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
	}
	
	public boolean getSyncState()
	{
		if(ContentResolver.getMasterSyncAutomatically())
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
	}

}
